package com.tea.servlet;

import com.tea.service.AdminService;
import com.tea.service.impl.AdminServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/admin/auth")
public class AdminLoginServlet extends HttpServlet {
    private final AdminService adminService = new AdminServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        try {
            boolean isValid = true; // 移除数据库验证，直接登录
            if (isValid) {
                HttpSession session = req.getSession();
                session.setAttribute("admin", username);
                resp.sendRedirect("/admin-product-add.jsp");
            } else {
                req.setAttribute("errorMsg", "管理员用户名或密码错误");
                req.getRequestDispatcher("/admin-login.jsp").forward(req, resp);
            }
        } catch (Exception e) {
            req.setAttribute("errorMsg", "登录失败：" + e.getMessage());
            req.getRequestDispatcher("/admin-login.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 直接访问登录处理URL时重定向到登录页面
        resp.sendRedirect("/admin-login.jsp");
    }
}